package com.example.service;

import com.example.entity.AddressBook;
import com.example.entity.Delivery;
import com.example.entity.Goods;
import com.example.entity.Order;
import com.example.enums.OrderStatusEnum;
import com.example.mapper.*;
import com.example.vo.OrderVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class OrderService {

    @Resource
    OrderMapper orderMapper;
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    CustomerMapper customerMapper;
    @Resource
    DeliveryMapper deliveryMapper;
    @Resource
    AddressBookMapper addressBookMapper;
    public void add(OrderVO orderVO) {
        Integer customerId = customerMapper.selectByUserId(orderVO.getUserId()).getCustomerId();
        Goods goods = Goods.builder()
                .customerId(customerId)
                .goodsName(orderVO.getGoodsName())
                .category(orderVO.getCategory())
                .description(orderVO.getDescription())
                .unit(orderVO.getUnit())
                .weight(orderVO.getWeight())
                .price(orderVO.getPrice())
                .build();
        goodsMapper.insert(goods);
        Order order = Order.builder()
                .customerId(customerId)
                .goodsId(goods.getGoodsId())
                .shippingBookId(orderVO.getShippingBookId())
                .deliveryBookId(orderVO.getDeliveryBookId())
                .totalAmount(orderVO.getPrice())
                .distance(orderVO.getDistance())
                .status(orderVO.getStatus())
                .paymentMethod(orderVO.getPaymentMethod())
                .remarks(orderVO.getRemark())
                .build();
        orderMapper.insert(order);
    }

    public void deleteById(Integer id) {
        orderMapper.deleteById(id);
    }

    public void updateById(Order order) {
        if(order.getStatus().equals(OrderStatusEnum.PAID.getStatusId())){
            Goods goods = goodsMapper.selectById(order.getGoodsId());
            AddressBook shippingBook = addressBookMapper.selectById(order.getShippingBookId());
            AddressBook deliveryBook = addressBookMapper.selectById(order.getDeliveryBookId());
            Delivery delivery = Delivery.builder()
                    .orderId(order.getOrderId())
                    .goodsId(goods.getGoodsId())
                    .originAddress(shippingBook.getAddress())
                    .originLongitude(shippingBook.getLongitude())
                    .originLatitude(shippingBook.getLatitude())
                    .currentAddress(shippingBook.getAddress())
                    .currentLongitude(shippingBook.getLongitude())
                    .currentLatitude(shippingBook.getLatitude())
                    .destinationAddress(deliveryBook.getAddress())
                    .destinationLongitude(deliveryBook.getLongitude())
                    .destinationLatitude(deliveryBook.getLatitude())
                    .plannedDate(LocalDate.now().plusDays(1))
                    .duration(3)
                    .distance(order.getDistance())
                    .build();
            deliveryMapper.insert(delivery);
        }
        orderMapper.updateById(order);
    }

    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }

    public Order selectById(Integer id) {
        return orderMapper.selectById(id);
    }

    public PageInfo<Order> selectPage(Integer pageNum, Integer pageSize) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Order> list = orderMapper.selectAll();
        return PageInfo.of(list);
    }
}